package com.ruoyi.web.controller.platform;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.system.domain.MonitorConfig;
import com.ruoyi.system.service.MonitorConfigService;
import com.ruoyi.system.service.MonitorRoleService;
import com.ruoyi.web.dto.MonitorRoleUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/monitor/role")
public class MonitorRoleController extends BaseController {

    @Autowired
    private MonitorRoleService monitorRoleService;
    @Autowired
    private MonitorConfigService monitorConfigService;

    @GetMapping("/all")
    public AjaxResult all() {
        List<MonitorConfig> allConfig = monitorConfigService.validList();
        List<TreeSelect> treeSelectList = allConfig.stream().map(config -> {
            TreeSelect tree = new TreeSelect();
            tree.setId(config.getId());
            tree.setLabel(config.getName());
            return tree;
        }).collect(Collectors.toList());
        return success(treeSelectList);
    }

    @GetMapping("/selectList/{roleId}")
    public AjaxResult selectList(@PathVariable("roleId") Long roleId) {
        List<Long> list = monitorRoleService.selectList(roleId);
        return success(list);
    }

    @PutMapping("/update")
    public AjaxResult update(@Valid @RequestBody MonitorRoleUpdateDto dto) {
        monitorRoleService.update(dto.getRoleId(), dto.getMenuIds());
        return success();
    }

}
